//
//  YXUserAccount.swift
//  微博YX
//
//  Created by apple on 15/11/25.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit


class YXUserAccount: NSObject, NSCoding {
    
    
    // MARK: - 属性
    /// 保存过期时间NSDate
    var expiresDate: NSDate?
    
    /// 当前授权用户的UID。
    var uid: String?
    
    /// 昵称
    var screen_name: String?
    
    /// 头像
    var avatar_large: String?
    
    /// 是否登录
    class var userLogin: Bool {
        get {
            return YXUserAccount.loadUserAccount() != nil
        }
    }
    /// 用于调用access_token
    var access_token: String?
    
    /// access_token的生命周期
    var expires_in: NSTimeInterval = 0 {
        didSet {
            // 设置过期时间
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    

    
   
    
    /// 字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    /// 加载用户信息
    func loadUserInfo() {
        // 获取用户数据
        YXNetworkTool.sharedInstance.loadUserInfo{ (result, error) -> () in
            if error != nil || result == nil {

                return
            }
                 // 设置属性
            self.screen_name = result!["screen_name"] as? String
            self.avatar_large = result!["avatar_large"] as? String
            
            // userAccount账户属性
            YXUserAccount.userAccount = self
            
            // 保存
            self.saveAccout()
        }
    }
    
    // 保存路径
    static let plistPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! + "/account.plist"
    
    /// 保存到沙盒
    func saveAccout() {
        // 归档
        NSKeyedArchiver.archiveRootObject(self, toFile: YXUserAccount.plistPath)
    }
    
    
    /// 内存中的账户.
    private static var userAccount: YXUserAccount?
    
    /// 读取
    class func loadUserAccount() -> YXUserAccount? {
        // 1.判断内存中的账户
        if userAccount == nil {
           
            userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(plistPath) as? YXUserAccount

        } else {

        }
        
        // 2.判断内存中是否有用账户,有账户,判断时间是否过期
        if userAccount != nil && userAccount?.expiresDate?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
          
            return userAccount
        }
        
        return nil
    }
    
    /// 解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    /// 归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    
    /// 找不到对应的属性
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    /// 打印
    override var description: String {
        return "access_token: \(access_token), expires_in:\(expires_in), uid = \(uid), expiresDate:\(expiresDate)"
    }
}
